package design.leetcode_155;

public class CustomStack {
    class MinStack {
        class Node {
            int value;
            int min;
            Node next;

            public Node(int value, int min) {
                this.value = value;
                this.min = min;
            }
        }

        Node head;

        /**
         * initialize your data structure here.
         */
        public MinStack() {
            head = null;
        }

        public void push(int val) {
            if (head == null) {
                head = new Node(val, val);
            } else {
                Node node = new Node(val, Math.min(head.min, val));
                node.next = head;
                head = node;
            }
        }

        public void pop() {
            if (head != null) {
                Node temp = head.next;
                head.next = null;
                head = temp;
            }
        }

        public int top() {
            if (head != null) {
                return head.value;
            }
            throw new RuntimeException("栈为空! 非法操作!");
        }

        public int getMin() {
            if (head != null) {
                return head.min;
            }
            throw new RuntimeException("栈为空! 非法操作!");
        }
    }
}
